<html style="height: 100%">
<head><#include "/base/head.html">
</head>
<body style="height: 100%">
	<div style="padding: 10px; height: 95%;">
		<div style="height: 25%;">
			<form id="diseaseForm" method="post" action="${contextPath}/expert/queryDisease">
				<table cellpadding="8">
					<tbody>
						<tr>
							<td>ID:</td>
							<td><input class="easyui-textbox" type="text" name="id" /></td>
							<td>疾病名称:</td>
							<td><input class="easyui-textbox" type="text" name="name" /></td>
							<td>严重程度:</td>
							<td><input class="easyui-textbox" type="text" name="crificality" /></td>
						</tr>

					</tbody>
				</table>
			</form>
			<div style="text-align: left; padding: 5px">
				<a id="diseaseSearchSubmit" href="javascript:void(0)" class="easyui-linkbutton mediumButton">查询</a>
			</div>
		</div>
		<script type="text/javascript">
			$(function() {
				init();

				var deleteUrl = "";

				$("#diseaseSearchSubmit").click(function() {
					refreshGrid();
				});
				//点击编辑用户
				$("#diseaseEdit").click(function() {

					var selected = $("#diseaseGrid").datagrid('getSelected');
					if (selected == null) {
						alert("请先选择疾病");
						return;
					}

					$("#diseaseFormEdit").attr("action", $(this).attr("postUrl"));
					$('#diseaseFormDlg').dialog({
						"iconCls" : "icon-edit",
						"title" : "疾病修改"
					});
					$('#diseaseFormDlg').dialog('open');

					var params = new Object();
					params.id = selected.id;
					$("#disease_id").val(selected.id);
					console.log($("#disease_id").find(":input[name=id]"));
					BaseJs.setFormByUrl("#diseaseFormEdit", params);
				});
				//初始化页面
				function init() {
					$("#diseaseFormEdit").form('clear');
					$("#diseaseForm").form('clear');

					refreshGrid();
				}

				//根据form里面的信息获取并填充grid
				function refreshGrid() {
					BaseJs.formChangeGrid("#diseaseForm", "#diseaseGrid");
				}

				$("#diseaseAdd").click(function() {

					$("#diseaseFormEdit").form('clear');
					$("#diseaseFormEdit").attr("action", $(this).attr("postUrl"));
					$('#diseaseFormDlg').dialog({
						"iconCls" : "icon-add",
						"title" : "疾病新增"
					});

					$("#disease_id").val("");
					$('#diseaseFormDlg').dialog('open');
				});

				$("#diseaseDelete").click(function() {

					var selected = $("#diseaseGrid").datagrid('getSelected');
					if (selected == null) {
						alert("请先选择疾病");
						return;
					}
					var params = new Object();
					params.id = selected.id;
					BaseJs.ajax($(this).attr('postUrl'), params, function() {
						refreshGrid();
						alert("删除疾病成功");
					})

				});
				//修改的弹出框按提交
				$("#diseaseSubmitEdit").click(function() {
					BaseJs.submitForm("#diseaseFormEdit", function(rData) {
						//修改完重新查询
						$("#diseaseSearchSubmit").click();
						$('#diseaseFormDlg').dialog('close');
					});
				});
			});
		</script>
		<div style="height: 75%">
			<div id="diseaseToolbar">
				<a id="diseaseAdd" postUrl="expert/addUpdateDisease" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-add" plain="true">新增疾病</a> <a id="diseaseEdit" postUrl="expert/addUpdateDisease" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-edit" plain="true">编辑疾病</a> <a id="diseaseDelete" postUrl="expert/deleteDisease" href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-remove" plain="true">删除疾病</a>
			</div>

			<div id="diseaseFormDlg" class="easyui-dialog" title="疾病新增" data-options="iconCls:'icon-add',closed: true" style="width: 400px; height: 400px; padding: 10px; text-align: center;">
				<!-- 弹出框用来增加和编辑-->
				<form id="diseaseFormEdit" method="post" action="" loadAction="expert/getDisease">
					<table align="center" cellpadding="8">

						<tr>
							<td>疾病名:</td>
							<td><input class="easyui-textbox" type="text" name="name" /></td>
						</tr>
						<tr>
							<td>描述:</td>
							<td><div>
									<input class="easyui-textbox" type="text" name="description" />
								</div></td>
						</tr>
						<tr>
							<td>严重程度:</td>
							<td><div>
									<input class="easyui-textbox" type="text" name="crificality" />
								</div></td>
						</tr>
						<tr>
							<td>治疗方法:</td>
							<td><div>
									<input class="easyui-textbox" type="text" name="solution" />
								</div></td>
						</tr>

					</table>
					<input type="hidden" id="disease_id" name="id">
				</form>

				<div style="text-align: center; margin: 10px">
					<a id="diseaseSubmitEdit" href="javascript:void(0)" class="easyui-linkbutton mediumButton">确定</a> &nbsp; <a href="javascript:void(0)" class="easyui-linkbutton mediumButton" onclick=" $('#diseaseFormDlg').dialog('close');">取消</a>
				</div>
			</div>


			<table id="diseaseGrid" toolbar="#diseaseToolbar" class="easyui-datagrid" title="症状显示" style="width: 100%; height: 100%;" data-options="singleSelect:true,method:'post',fitColumns : true">

				<thead>
					<tr>
						<th data-options="field:'id',width:50,align:'center'">疾病ID</th>
						<th data-options="field:'name',width:100,align:'center'">疾病名</th>
						<th data-options="field:'description',width:100,align:'center'">描述</th>
						<th data-options="field:'crificality',width:100,align:'center'">严重程度</th>
						<th data-options="field:'solution',width:100,align:'center'">治疗方法</th>
						<th data-options="field:'disease-symptom',width:100,align:'center',formatter:setDiseaseSymptom">疾病-症状</th>

						<script>
							function setDiseaseSymptom(val, data) {
								return "<a  class='editLink' diseaseId='" + data.id + "' href='#'>编辑</a>"
							}

							$(document).on("click", ".editLink", function() {
								var diseaseId = $(this).attr("diseaseId");
								$("#linkDiseaseId").val(diseaseId)
								//console.log(lovId);
								//refreshLinks(lovId);
								updateLinkGrid(diseaseId);
								$('#linkEditDlg').dialog({
									title : '规则编辑',
									width : 400,
									height : 400,
									closed : false,
									modal : true
								});
							});
	
							var lovRoleNameMapping = new Object();
							var lovUserStatusMapping = new Object();

							function updateRoleInfo(data, e) {

								if (data == 3 || data == 4) {
									return "<a class='setRoleInfo' roleId='"+data+"' userId='"+e.id+"' href='##' title='点击设置'>" + lovRoleNameMapping[data] + "</a>";
								} else {
									return lovRoleNameMapping[data];
								}
							}
							function formatUserStatus(data) {
								return lovUserStatusMapping[data];
							}

							$(function() {

								//initSelect();

								function initSelect() {
									//初始化lovRoleNameMapping 用来显示select的列
									$("#diseaseForm_authority").combobox();
									var data = $("#diseaseForm_authority").combobox("getData");
									var textField = $("#diseaseForm_authority").combobox("options").textField;
									var valueField = $("#diseaseForm_authority").combobox("options").valueField;

									for ( var i in data) {
										lovRoleNameMapping[data[i][valueField]] = data[i][textField];
									}

									//初始化状态
									var statusLov = '{' + '${lovService.getLovListString("lov_user_status")}' + '}';
									var statusObj = JSON.parse(statusLov);
									data = statusObj.data;
									textField = statusObj.textField;
									valueField = statusObj.valueField;
									for ( var i in data) {
										lovUserStatusMapping[data[i][valueField]] = data[i][textField];
									}
								}

								$(document).on('click', ".setRoleInfo", function() {

									var roleId = $(this).attr('roleId');
									var userId = $(this).attr('userId');
									var roleName = "";

									switch (roleId) {
									case '3':
										openUserExtendEdit('member', userId);
										break;
									case '4':
										openUserExtendEdit('doctor', userId);
										break;
									}

								});
								var userExtendEditDlg = new Object();

								function openUserExtendEdit(roleName, userId) {
									console.log(roleName + "  " + userId);
									if (!userExtendEditDlg[roleName]) {
										userExtendEditDlg[roleName] = $('#' + roleName + 'EditDlgPanel').panel({
											href : '${contextPath}/user/' + roleName + '_edit.html',
											onLoad : function() {

												$("#" + roleName + "Id").val(userId);
												$("#" + roleName + "EditDlg").dialog('open');
											}
										});
									}

									$("#" + roleName + "Id").val(userId);
									$("#" + roleName + "EditDlg").dialog('open');
								}

							});
							
							
							var editLinkIndex = undefined;
							function endEditingLink() {
								if (editLinkIndex == undefined) {
									return true
								}
								if ($('#linkEditGrid').datagrid('validateRow', editLinkIndex)) {
									$('#linkEditGrid').datagrid('endEdit', editLinkIndex);
									editLinkIndex = undefined;
									return true;
								} else {
									return false;
								}
							}

							function onClickCellLink(index, field) {
								if (editLinkIndex != index) {
									if (endEditingLink()) {
										$('#linkEditGrid').datagrid('selectRow', index).datagrid('beginEdit', index);
										var ed = $('#linkEditGrid').datagrid('getEditor', {
											index : index,
											field : field
										});
										if (ed) {
											($(ed.target).data('textbox') ? $(ed.target).textbox('textbox') : $(ed.target)).focus();
										}
										editLinkIndex = index;
									} else {
										setTimeout(function() {
											$('#linkEditGrid').datagrid('selectRow', editLinkIndex);
										}, 0);
									}
								}
							}
							//保存Link的设置
							function acceptLink() {
								if (endEditingLink()) {
									var rows = $('#linkEditGrid').datagrid('getChanges');
									var lovId = $("#linkEditGrid").attr("lovId");

									if(lovId == null) {
										alert("保存失败!")
										return ;
									}
										
									for(var i = 0 ;i<rows.length ;i++) {
										rows[i].lovId = lovId;
									}
									console.log(rows);
									var jsonRows = JSON.stringify(rows);
									
									$('#linkEditGrid').datagrid('acceptChanges');

									BaseJs.ajax("${contextPath}/sys/Link/addUpdateLink", {lovsItemStr:jsonRows}, function(){
										$("#lovSearchSubmit").click();
									});
									
									refreshLinks();
									
								}else {
									console.log("保存失败");
								}
							}

							function endEditingLink() {
								if (editLinkIndex == undefined) {
									return true
								}
								if ($('#linkEditGrid').datagrid('validateRow', editLinkIndex)) {
									$('#linkEditGrid').datagrid('endEdit', editLinkIndex);
									editLinkIndex = undefined;
									return true;
								} else {
									return false;
								}
							}

							
							function removeLink() {

								var res = confirm("确定要删除吗?");
								if(res) {
									var row = $('#linkEditGrid').datagrid('getSelected');

									var id = row.id;
									BaseJs.ajax("${contextPath}/expert/symptom/deleteSymptomsLink", {id:id}, function(){
										var diseaseId = $("#linkDiseaseId").val();
										updateLinkGrid(diseaseId);
									})
									
								}
							}
							//撤销Link
							function rejectLink() {
					            $('#linkEditGrid').datagrid('rejectChanges');
					            editLinkIndex = undefined;
					        }
							
							
							function refreshLinks(lovId) {
								$('#linkEditGrid').datagrid({data:[]})
								if(lovId == null)
									lovId = $("#linkEditGrid").attr("lovId");
								
								if(lovId == null) {
									alert("刷新失败");
									return ;
								} 
									
								//BaseJs.ajax("${contextPath}/sys/Link/queryLinks", {lovId:lovId}, function(r){
								//	$('#linkEditGrid').datagrid( {data:r.data} );
								//});
							}
							$(function(){
							    $("input",$("#linkSymptomId").next("span")).click(function(){
							        alert("ok");
									$('#dd').dialog({
									    title: '症状选择框',
									    width: 800,
									    height: 600,
									    closed: false,
									    cache: false,
									    modal: true
									});
							    });
							    
							    
							});
							
							function updateLinkGrid(diseaseId) {
								BaseJs.ajax("${contextPath}/expert/symptom/getSymptomsLink?id="+diseaseId,null,function(rData){
									console.log(rData.data);
									$("#linkEditGrid").datagrid({
										data : rData.data
									});
								});
							}
							
							//加Link
							function appendLink() {
								
								 var param = new Object();
							 
								param.cf = $("#linkSymptomCF").textbox('getValue');
								param.symptomId = $("#linkSymptomId").textbox('getValue');
								param.diseaseId = $("#linkDiseaseId").val();
								if(!param.symptomId) {
									alert("请先选择症状");
									return ;
								}
								if(!param.diseaseId) {
									alert("请填写CF值");
									return ;
								}
								console.log(param);
								
								var data = $("#linkEditGrid").datagrid("getData");
								var sum = 0;
								for(var i = 0 in data.rows) {
									sum = sum + data.rows[i].cf;
								}

								if(sum + Number(param.cf) > 100) {
									alert("cf值相加不能大于100, 当前新增的cf值只能 <= "+(100-sum));
									return ;
								} 

								
								BaseJs.ajax("${contextPath}/expert/symptom/addSymptomsLink?",param,function(rData){
									console.log(rData.data);
									$("#linkEditGrid").datagrid({
										data : rData.data
									});
									updateLinkGrid(param.diseaseId);
								});	 
							}
						</script>
				</thead>
			</table>
		</div>
	</div>
	
	<div id="linkEditToolbar">
		<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="removeLink()">删除</a> <br />
		 症状id:<input class="easyui-textbox" type="text" readonly="readonly" id="linkSymptomId" name="symptomId" value=""> <br /> 
		 CF值 : <input class="easyui-textbox" type="number" id="linkSymptomCF" name="cf"> 
		 <input type="hidden" id="linkDiseaseId"/>
		<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="appendLink()">添加</a>
	</div>
	
	<div id="linkEditDlg">
		<table id="linkEditGrid" toolbar="#linkEditToolbar" class="easyui-datagrid" style="width: 100%; height: 100%;" data-options="singleSelect:true,method:'post',fitColumns : true">
			<thead>
				<tr>
					<th data-options="field:'id',width:50,align:'center'">id</th>
					<th data-options="field:'name',width:100,align:'center',editor:'textbox'">症状名</th>
					<th data-options="field:'cf',width:100,align:'center',editor:'textbox'">症状描述</th>
				</tr>
			</thead>
		</table>
	</div>


	<div id="dd" class="easyui-dialog" title="My Dialog" style="width: 400px; height: 200px; "   data-options="iconCls:'icon-save',resizable:true,modal:true,closed:true">

		<form id="symptomForm" method="post" action="${contextPath}/expert/querySymptom">
			<table cellpadding="8">
				<tbody>
					<tr>
						<td>ID:</td>
						<td><input class="easyui-textbox" type="text" name="id" /></td>
						<td>症状名:</td>
						<td><input class="easyui-textbox" type="text" name="name" /></td>
						<td>级别:</td>
						<td><input class="easyui-textbox" type="text" name="level" /></td>
					</tr>

				</tbody>
			</table>
		</form>
		<div style="text-align: left; padding: 5px">
			<a id="symptomSearchSubmit" href="javascript:void(0)" class="easyui-linkbutton mediumButton">查询</a>
		</div>
		<table id="symptomGrid" toolbar="#symptomToolbar" class="easyui-datagrid" title="症状显示" data-options="singleSelect:true,method:'post',fitColumns : true">
			<thead>
				<tr>
					<th data-options="field:'id',width:50,align:'center'">症状ID</th>
					<th data-options="field:'name',width:100,align:'center'">症状名</th>
					<th data-options="field:'description',width:100,align:'center'">描述</th>
					<th data-options="field:'level',width:100,align:'center'">级别</th>
					<th data-options="field:'parentId',width:100,align:'center'">父类</th>
					<th data-options="field:'frequency',width:100,align:'center'">次数</th>
			</thead>
		</table>
		<script>
			//根据form里面的信息获取并填充grid
			$(function(){
				
				$("#symptomGrid").datagrid({
					onDblClickRow:function(index,field,value){
					//alert("adsf");
				
				//	linkSymptomId
				console.log(field['id']);

					$("#linkSymptomId").textbox('setValue', field['id']);
					$('#dd').dialog('close');
					//$(this).datagrid('beginEdit', index);
					//var ed = $(this).datagrid('getEditor', {index:index,field:field});
					$//(ed.target).focus();
				}});
 
				$("#symptomSearchSubmit").click(function(){
					refreshGrid();
				});
			});
			
			function refreshGrid() {
				BaseJs.formChangeGrid("#symptomForm", "#symptomGrid");
			}
			
			refreshGrid();
			</script>
	</div>

</body>
</html>